﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Transactions;

namespace SMS.Client
{
    public partial class ClientTransactions : System.Web.UI.Page
    {
        SMS.Business.Customer.CustomerBussiness CustBuss;
        SMS.Business.Customer.CustomerTransactionsBusiness CustTransBuss;
        public List<SMS.DAL.CutomerCreditTransaction> Trans
        {
            set
            {
                ViewState["TransFound"] = value;
            }
            get
            {
                return (List<SMS.DAL.CutomerCreditTransaction>)ViewState["TransFound"];
            }

        }
        public SMS.DAL.User CurrentUser { get { return (SMS.DAL.User)Session["CurrentUser"]; } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurrentUser"] == null)
                Response.Redirect(@"~\Account\Login.aspx");
            CustBuss = new Business.Customer.CustomerBussiness();
            CustTransBuss = new Business.Customer.CustomerTransactionsBusiness();
            if (!IsPostBack)
            {
                this.ddlCustomers.DataSource = CustBuss.GetAll();
                this.ddlCustomers.DataTextField = "Name";
                this.ddlCustomers.DataValueField = "ID";
                this.ddlCustomers.DataBind();
                if (CurrentUser.RoleID != 1)//Admin
                {
                    GridView1.Columns[GridView1.Columns.Count - 1].Visible = false;
                    GridView1.Columns[GridView1.Columns.Count - 2].Visible = false;
                }
                if (Request.QueryString["ClientID"] != null)
                {
                    this.ddlCustomers.Items.FindByValue(Request.QueryString["ClientID"]).Selected = true;
                    Trans = CustTransBuss.Search(new DAL.Providers.CustomerCreditTransSearch() { CustomerID = int.Parse(ddlCustomers.SelectedValue) });
                    this.GridView1.DataSource = Trans;
                    GridView1.DataBind();
                }

            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Trans = CustTransBuss.Search(new DAL.Providers.CustomerCreditTransSearch()
            {
                CustomerID = int.Parse(ddlCustomers.SelectedValue),
                StartDate = txtDateFrom.Text == String.Empty ? (DateTime?)null : DateTime.Parse(txtDateFrom.Text),
                EndDate = txtDateTo.Text == String.Empty ? (DateTime?)null : DateTime.Parse(txtDateTo.Text),
            });
            this.GridView1.DataSource = Trans;
            GridView1.DataBind();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            bool success = false;
            using (TransactionScope scp = new TransactionScope())
            {
                try
                {

                    SMS.DAL.CutomerCreditTransaction temp = (SMS.DAL.CutomerCreditTransaction)GridView1.Rows[e.RowIndex].DataItem;
                    Trans.Remove(temp);
                    SMS.Business.Customer.CustomerBussiness CB = new Business.Customer.CustomerBussiness();
                    SMS.DAL.Customer Cust = CB.GetByID((int)temp.CustomerID);
                    if (temp.Credit == null)
                        Cust.Credit -= (decimal)temp.Debit;
                    else
                        Cust.Credit += (decimal)temp.Credit;
                    CB.Update(Cust);
                    CustTransBuss.Delete((int)e.Keys["ID"]);

                    this.GridView1.DataSource = Trans;
                    GridView1.DataBind();
                    scp.Complete();
                    success = true;
                }
                catch (Exception EX)
                {

                }
            }
            if (success)
            {

            }
                
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            this.GridView1.DataSource = Trans;
            this.DataBind();
        }
    }
}